/* eslint-disable no-unused-vars */
import { useCallback, useEffect, useState } from 'react';
import _ from 'lodash';
// eslint-disable-next-line no-unused-vars
const useTableGetData = (initFn, call, query, change) => {
  const [page, setpage] = useState({ page: 1, pageSize: 10 });
  const [total, setTotal] = useState(0);
  const changePage = useCallback((pagination, pageInfo) => {
    setpage({
      page: pagination.current,
      pageSize: pagination.pageSize
    });
  }, []);
  const [load, setload] = useState(false);
  const [tableData, settableData] = useState([]);
  const fetchData = async () => {
    setload(true);
    let params = { ...page, ...query };
    const result = await initFn(params);
    setload(false);
    if (_.get(result, 'data.data')) {
      if (call) {
        settableData(call(result));
      } else {
        let table = _.get(result, 'data.data.list', []);
        setTotal(_.get(result, 'data.data.count', 0));
        settableData(table);
      }
    }
  };
  useEffect(() => {
    fetchData();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [page]);

  useEffect(() => {}, [change]);
  return { page, load, changePage, tableData, total };
};
export default useTableGetData;
